package org.moyrax.compressor;

import org.mozilla.javascript.EvaluatorException;

/**
 * Thrown when there's some error in the compression operation.
 *
 * @author Matias Mirabelli &lt;lumen.night@gmail.com&gt;
 * @since 0.1.0
 */
public class CompressorException extends EvaluatorException {
  /** Id for serialization. */
  private static final long serialVersionUID = -5770158823774510996L;

  /**
   * Constructs a new compressor exception. This kind of exception is commonly
   * thrown when there're some error in the source file that's being proccessed.
   *
   * @param message     A String describing the error.
   * @param sourceName  A String describing the source where the error occured;
   *                    typically a filename or URL.
   * @param line        The line number associated with the error.
   * @param lineSource  The text of the line (may be null).
   * @param lineOffset  The offset into lineSource where problem was detected.
   */
  public CompressorException(final String detail, final String sourceName,
      final int lineNumber, final String lineSource, final int lineOffset) {

    super(detail, sourceName, lineNumber, lineSource, lineOffset);
  }

  /**
   * Constructs a new compressor exception. This kind of exception is commonly
   * thrown when there're some error in the source file that's being proccessed.
   *
   * @param message     A String describing the error.
   * @param sourceName  A String describing the source where the error occured;
   *                    typically a filename or URL.
   * @param lineNumber  The line number associated with the error.
   */
  public CompressorException(final String message, final String sourceName,
      final int lineNumber) {

    super(message, sourceName, lineNumber);
  }

  /**
   * Constructs a new compressor exception. This kind of exception is commonly
   * thrown when there're some error in the source file that's being proccessed.
   *
   * @param message A String describing the error.
   */
  public CompressorException(final String message) {
    super(message);
  }

  /**
   * Constructs a new compressor exception. This kind of exception is commonly
   * thrown when there're some error in the source file that's being proccessed.
   *
   * @param message A String describing the error.
   * @param cause   Exception that caused this CompressorException.
   */
  public CompressorException(final String message, final Throwable cause) {
    super(message);

    this.initCause(cause);
  }

  /**
   * Constructs a new compressor exception from a known cause.
   *
   * @param cause  Wrapped exception.
   */
  public CompressorException(final EvaluatorException cause) {
    super(cause.details(), cause.sourceName(), cause.lineNumber(),
        cause.lineSource(), cause.columnNumber());
  }

  /**
   * Returns a detailed description of the error thrown in the script that's
   * currently being proccessed.
   */
  public String toString() {
    final String message = "Error evaluating file '" + this.sourceName() +
      "' on line " + this.lineNumber() + " [column " + this.columnNumber() +
      "]: " + this.details() + "\n\t" + this.lineSource();

    return message;
  }
}
